package com.joker.orange.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.joker.common.core.page.TableDataInfo;
import com.joker.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.joker.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.joker.orange.domain.bo.HotSearchHistoryBo;
import com.joker.orange.domain.vo.HotSearchHistoryVo;
import com.joker.orange.domain.HotSearchHistory;
import com.joker.orange.mapper.HotSearchHistoryMapper;
import com.joker.orange.service.IHotSearchHistoryService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 热搜和历史Service业务层处理
 *
 * @author Joker
 * @date 2024-07-04
 */
@RequiredArgsConstructor
@Service
public class HotSearchHistoryServiceImpl implements IHotSearchHistoryService {

    private final HotSearchHistoryMapper baseMapper;

    /**
     * 查询热搜和历史
     */
    @Override
    public HotSearchHistoryVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询热搜和历史列表
     */
    @Override
    public TableDataInfo<HotSearchHistoryVo> queryPageList(HotSearchHistoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HotSearchHistory> lqw = buildQueryWrapper(bo);
        Page<HotSearchHistoryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询热搜和历史列表
     */
    @Override
    public List<HotSearchHistoryVo> queryList(HotSearchHistoryBo bo) {
        LambdaQueryWrapper<HotSearchHistory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HotSearchHistory> buildQueryWrapper(HotSearchHistoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HotSearchHistory> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getSearchName()), HotSearchHistory::getSearchName, bo.getSearchName());
        lqw.eq(StringUtils.isNotBlank(bo.getSearchType()), HotSearchHistory::getSearchType, bo.getSearchType());
        return lqw;
    }

    /**
     * 新增热搜和历史
     */
    @Override
    public Boolean insertByBo(HotSearchHistoryBo bo) {
        HotSearchHistory add = BeanUtil.toBean(bo, HotSearchHistory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改热搜和历史
     */
    @Override
    public Boolean updateByBo(HotSearchHistoryBo bo) {
        HotSearchHistory update = BeanUtil.toBean(bo, HotSearchHistory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HotSearchHistory entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除热搜和历史
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean deleteSearchHistory() {
        return baseMapper.delete(new LambdaQueryWrapper<HotSearchHistory>()
            .eq(HotSearchHistory::getSearchType,"1")) > 0;
    }
}
